/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgrail.pilot.app.command.cmo.OkrPeriodButtonCmd;
import com.itgrail.pilot.app.command.cmo.query.PageOkrPeriodCmd;
import com.itgrail.pilot.app.command.co.OkrPeriodDetailCO;
import com.itgrail.pilot.app.converter.OkrPeriodClientConverter;
import com.itgrail.pilot.domain.user.enums.PeriodStatusEnum;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DictTypeMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.OkrPeriodMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DictTypeDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.OkrPeriodDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;

import java.util.List;


@CmdHandler
public class PageOkrPeriodCmdExe implements CommandExecutorI<ResultData<PageResult<OkrPeriodDetailCO>>, PageOkrPeriodCmd> {
    @Autowired
    private OkrPeriodMapper mapper;

    @Autowired
    private OkrPeriodClientConverter converter;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Override
    public ResultData execute(PageOkrPeriodCmd cmd) {
        ResultData resultData = ResultData.success(null);
        check(cmd);
        IPage<OkrPeriodDO> page = page(cmd);
        PageResult<OkrPeriodDetailCO> pageResult = new PageResult<>();
        List<OkrPeriodDetailCO> detailCOS = converter.dataToClients(page.getRecords());
        for (OkrPeriodDetailCO detailCO : detailCOS) {
            if (detailCO.getCreated() != null) {
                LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(UserDO::getUserId, detailCO.getCreated());
                UserDO userDO = userMapper.selectOne(queryWrapper);
                if (userDO != null) {
                    detailCO.setCreateUser(userDO.getName());
                }
                if (null == detailCO.getOkrPeriodButtonCmd()) {
                    detailCO.setOkrPeriodButtonCmd(new OkrPeriodButtonCmd());
                }
            }
            DictTypeDO dictTypeDO = dictTypeMapper.selectById(detailCO.getIsEdit());
            if (dictTypeDO != null) {
                detailCO.setPeriodStatus(PeriodStatusEnum.getDescByCode(dictTypeDO.getDictCode()));
            }
        }
        pageResult.setCurrentPage(page.getCurrent());
        pageResult.setTotalCount(page.getTotal());
        pageResult.setTotalPage(page.getPages());
        pageResult.setList(detailCOS);
        resultData.setData(pageResult);
        return resultData;
    }

    protected IPage<OkrPeriodDO> page(PageOkrPeriodCmd cmd) {
        Page<OkrPeriodDO> page = new Page<>(cmd.getPageNo(), cmd.getPageSize());
        QueryWrapper<OkrPeriodDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("show_enable", 0);
        if (StringUtils.isNotEmpty(cmd.getKey())) {
            queryWrapper.like("name", cmd.getKey());
        }
        queryWrapper.orderByDesc("id");
        return mapper.selectPage(page, queryWrapper);
    }

    protected boolean check(PageOkrPeriodCmd cmd) {
        return true;
    }

}
